package com.jianyun.jyzs.http;


import com.jianyun.jyzs.bean.CrmDetial;
import com.jianyun.jyzs.bean.CrmListResponse;
import com.jianyun.jyzs.dao.CrmDao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.jianyun.jyzs.utils.RceLog;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by mlli on 2017/11/21.
 */

public class CrmSynachroHelper {

    private static int tag = 0;

    public static void getCrmDetial(final CrmDao dao, final List<CrmListResponse.CrmListBean> selectedList, String userId, final CrmSyncListener listener) {
        /*try {


            Api api = RetrofitHelper.builderbch().create(Api.class);
            tag = 0;
            for (CrmListResponse.CrmListBean l : selectedList) {
                RceLog.d("lml", "同步的数据id" + l.getId());
            }
            for (int i = 0; i < selectedList.size(); i++) {
                String id = selectedList.get(i).getId();
                api.getCrmDetial(userId, id)
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribeOn(Schedulers.io())
                        .subscribe(new CustomCallback<CrmDetial>() {
                            @Override
                            protected void onResponse(CrmDetial response) {
                                RceLog.d("lml", response.getPo().getName());
                                tag += 1;
                                if (response.isResult()) {
                                    dao.insertCrm(response.getPo());
                                }
                                if (tag == selectedList.size())
                                    listener.synchroOver(selectedList);
                            }

                            @Override
                            protected void onException(String msg) {
                                tag += 1;
                                RceLog.d("lml", msg);
                                if (tag == selectedList.size() & listener != null)
                                    listener.synchroOver(selectedList);

                            }
                        });
            }
        } catch (Exception e) {

        }*/
    }

    public static void searchCrmList(String userId, String searchtitle, String searchstime, String endtime, final CrmSyncListener listener) {
        /*try {
            HashMap<String, String> map = new HashMap<>();
            map.put("userId", userId);
            map.put("page", "1");
            if (searchtitle != null) map.put("name", searchtitle);
            if (searchstime != null) map.put("startDt", searchstime);
            if (endtime != null) map.put("endDt", endtime);
            Api api = RetrofitHelper.builderbch().create(Api.class);
            api.getCrmList(map).observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(new CustomCallback<CrmListResponse>() {
                        @Override
                        protected void onResponse(CrmListResponse response) {
                            RceLog.d("lml", "crm列表" + response);
                            if (response.isResult()) {
                                List<CrmListResponse.CrmListBean> list = response.getList();
                                if (listener != null & list != null) listener.searchCrm(list);
                            } else {
                                if (listener != null)
                                    listener.searchCrm(new ArrayList<CrmListResponse.CrmListBean>());
                            }
                        }

                        @Override
                        protected void onException(String msg) {
                            listener.getCrmlistFail(msg);
                        }
                    });
        } catch (Exception e) {

        }*/

    }

    public interface CrmSyncListener {

        void getCrmListSuccess(List<CrmListResponse.CrmListBean> list);

        void getCrmlistFail(String msg);

        void synchroOver(List<CrmListResponse.CrmListBean> removeindex);

        void searchCrm(List<CrmListResponse.CrmListBean> list);

    }

    public static void getCrmList(String userId, int page, final CrmSyncListener listener) {
        /*try {

            HashMap<String, String> map = new HashMap<>();
            map.put("userId", userId);
            map.put("page", page + "");
            Api api = RetrofitHelper.builderbch().create(Api.class);
            api.getCrmList(map)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(new CustomCallback<CrmListResponse>() {
                        @Override
                        protected void onResponse(CrmListResponse response) {
                            RceLog.d("lml", "crm列表" + response);
                            if (response.isResult()) {
                                List<CrmListResponse.CrmListBean> list = response.getList();
                                if (listener != null & list != null)
                                    listener.getCrmListSuccess(list);
                            } else {
                                if (listener != null)
                                    listener.getCrmListSuccess(new ArrayList<CrmListResponse.CrmListBean>());
                            }
                        }

                        @Override
                        protected void onException(String msg) {
                            listener.getCrmlistFail(msg);
                        }
                    });
        } catch (Exception e) {

        }*/
    }
}
